package com.breeze.toolkit.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by john on 2016/9/1.
 */
public class Util {
	private static final String FORMAT_YMD = "yyyy年MM月dd日";

	private Util() {}

	public static boolean isNetworkConnected(Context context) {
		if (context == null || context.getApplicationContext() == null) {
			return false;
		}

		ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(
				Context.CONNECTIVITY_SERVICE);
		if (manager == null) {
			return false;
		}

		NetworkInfo networkinfo = manager.getActiveNetworkInfo();

		return !(networkinfo == null || !networkinfo.isAvailable());
	}

	public static String getFormatDateString(long time) {
		return new SimpleDateFormat(FORMAT_YMD).format(new Date(time));
	}

	public static int getCount(List<?> list) {
		return list == null ? 0 : list.size();
	}

	public static <T> T getItem(List<T> list, int index) {
		return list == null || index >= list.size() || index < 0 ? null : list.get(index);
	}
}
